package k8s

import (
	"context"

	appsv1 "k8s.io/api/apps/v1"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	core "k8s.io/client-go/kubernetes/typed/apps/v1"
)

func NewStatefulSet(c *kubernetes.Clientset) *StatefulSet {
	return &StatefulSet{
		core: c.AppsV1(),
	}
}

type StatefulSet struct {
	core core.AppsV1Interface
}

// 获取StatefulSet列表
func (s *StatefulSet) GetStatefulSets(ctx context.Context, namespace string, req *v1.ListOptions) (*appsv1.StatefulSetList, error) {
	return s.core.StatefulSets(namespace).List(ctx, *req)
}

// 获取StatefulSet详情
func (s *StatefulSet) GetStatefulSetDetails(ctx context.Context, namespace, name string) (*appsv1.StatefulSet, error) {
	return s.core.StatefulSets(namespace).Get(ctx, name, v1.GetOptions{})
}

// 创建StatefulSet
func (s *StatefulSet) CreateStatefulSet(ctx context.Context, namespace string, statefulSet *appsv1.StatefulSet) (*appsv1.StatefulSet, error) {
	return s.core.StatefulSets(namespace).Create(ctx, statefulSet, v1.CreateOptions{})
}

// 删除StatefulSet
func (s *StatefulSet) DeleteStatefulSet(ctx context.Context, namespace, name string) error {
	return s.core.StatefulSets(namespace).Delete(ctx, name, v1.DeleteOptions{})
}

// 更新StatefulSet
func (s *StatefulSet) UpdateStatefulSet(ctx context.Context, namespace string, statefulSet *appsv1.StatefulSet) (*appsv1.StatefulSet, error) {
	return s.core.StatefulSets(namespace).Update(ctx, statefulSet, v1.UpdateOptions{})
}
